﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DataBuilder.SpellCode
{
    public partial class FrmSpellCodesBuilder : Form
    {
        public FrmSpellCodesBuilder()
        {
            InitializeComponent();
        }
        
        SpellServer sver = new SpellServer();
        
        SpellStrList list;

        int currentPageID = 1;

        int ItemsCount;

        int limited ;

        int PageSize ;

        int PageCount;


        private void button1_Click(object sender, EventArgs e)
        {
            currentPageID = 1;

            int length = GetLength();

            list = sver.GetSpellStr(length);

            ItemsCount = list.theList.Count;

            limited = GetLimmited();

            PageSize = GetPageSize();

            PageCount = GetPageCount(ItemsCount, PageSize);

            label2.Text = PageCount.ToString();

            PaintList(list, currentPageID);

        }

        private void PaintList(SpellStrList list, int pageID)
        {
            textBox1.Text = "";

            int startID = GetStartID(pageID, PageCount, PageSize, ItemsCount);

            int endID = GetEndID(pageID, PageCount, PageSize, ItemsCount);

            for (int i = startID; i < endID; i++)
            {
                if (list.theList[i].Length <= limited)
                {
                    textBox1.Text += list.theList[i] + "\r\n";
                }
            }

            label1.Text = pageID.ToString();
        }

        private int GetEndID(int pageID, int PageCount, int PageSize, int ItemsCount)
        {
            int EndID = GetStartID(pageID, PageCount, PageSize, ItemsCount) + PageSize;

            if (EndID>ItemsCount)
            {
                return ItemsCount;
            }
            return EndID;
        }

        private int GetStartID(int pageID, int PageCount, int PageSize, int ItemsCount)
        {
            if (pageID<=0)
	        {
                return 0;
	        }

            if (pageID==1)
            {
                return 0;
            }

            if (pageID>PageCount)
            {
                return GetStartID(PageCount,PageCount,PageSize,ItemsCount);
            }

            int StratID=(pageID-1)*PageSize;

            return StratID;
        }

        private int GetPageCount(int count, int PageSize)
        {
            int PageCount = count / PageSize;

            if (count % PageSize!=0)
            {
                PageCount++;
            }

            return PageCount;
        }

        private int GetPageSize()
        {
            return int.Parse(textBox4.Text.Trim());
        }

        private int GetLimmited()
        {
            return int.Parse(textBox3.Text.Trim());
        }

        private int GetLength()
        {
            return int.Parse(textBox2.Text.Trim());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            currentPageID = 1;
            PaintList(list, currentPageID);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            currentPageID = PageCount;
            PaintList(list, currentPageID);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            currentPageID--;

            if (currentPageID<1)
            {
                currentPageID = 1;
            }

            PaintList(list, currentPageID);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            currentPageID++;

            if (currentPageID>PageCount)
            {
                currentPageID = PageCount;
            }

            PaintList(list, currentPageID);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet(GetDataSetName());

            DataTable dt = new DataTable(GetTableName());

            dt.Columns.Add("SpellCode");

            for (int i = 0; i < list.theList.Count; i++)
            {
                if (list.theList[i].Length <= limited)
                {
                    dt.Rows.Add(list.theList[i]);
                }
            }

            ds.Tables.Add(dt);

            ds.WriteXml(GetSaveFilePath());

            MessageBox.Show("保存成功");
        }

        private string GetSaveFilePath()
        {
            return textBox_SavePath.Text.Trim();
        }

        private string GetTableName()
        {
            return textBox_TableName.Text.Trim();
        }

        private string GetDataSetName()
        {
            return textBox_DatasetName.Text.Trim();
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void buttonSaveTxt_Click(object sender, EventArgs e)
        {
            SaveTxtFile(GetSaveFilePath());
            MessageBox.Show("保存成功");
        }

        public void SaveTxtFile(string path)
        {
            StreamWriter streamw = File.CreateText(path);
            try
            {
                for (int i = 0; i < list.theList.Count; i++)
                {
                    if (list.theList[i].Length <= limited)
                    {
                        string text = list.theList[i];
                        
                        streamw.WriteLine(text);
                         
                    }
                }
                
                streamw.Close();
            }
            catch (IOException ex)
            {
                
            }
        }
    }
}
